//
//  JCLStatusViewModel.swift
//  WeiBo
//
//  Created by DDC on 2017/9/15.
//  Copyright © 2017年 DDC. All rights reserved.
//

import Foundation

/// 单条微博数据模型
class JCLStatusViewModel :CustomStringConvertible{

    
    /// 设置字体大小属性
    let originalFont = UIFont.systemFont(ofSize: 15)
    let retweetedFont = UIFont.systemFont(ofSize: 14)
    /// 微博模型
    var status:JCLStatus
    
    //会员图标
    var memberIcon:UIImage?
    //认证图标
    var grassroot:UIImage?
    /// 转发文字
    var reportsStr:String?
    /// 评论文字
    var commentStr:String?
    /// 点赞文字
    var likeStr:String?
    /// 配图视图大小属性
    var pictureSize = CGSize()
    /// 如果是被转发微博，原创微博一定没有图片
    var picURLs:[JCLStatusPicture]? {
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    /// 微博正文的属性文本
    var statusAttrText:NSAttributedString?
    /// 被转发微博的属性文本
    var retweetedAttrText:NSAttributedString?
    /// 缓存的微博行高的属性
    var rowHeight:CGFloat = 0
    /// 构造函数
    ///
    /// - Parameter model: 微博模型
    init(model: JCLStatus) {
        
        self.status = model
        //会员图标
        if (model.user?.mbrank)! > 0 && (model.user?.mbrank)! < 7 {
            
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
            memberIcon = UIImage(named: imageName)
        }
        //认证图标  认证类型 -1：没有认证 0：认证用户 2，3，5：企业认证 220：达人
        switch model.user?.verified_type ?? -1 {
        case 0:
            grassroot = UIImage(named: "avatar_vip")
        case 2,3,5:
            grassroot = UIImage(named: "avatar_enterprise_vip")
        case 7:
            grassroot = UIImage(named: "avatar_vgirl")
        case 220:
            grassroot = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        //底部工具栏显示文本样式
        reportsStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr: "赞")
        
        //配图视图大小设置
        pictureSize = calsPictureSize(count: picURLs?.count)
        //设置被转发微博的文本内容
        let rText = "@" + (status.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        // 设置转发微博的属性文本
        retweetedAttrText = JCLEmoticonsManager.shared.emoticonString(string: rText, font: retweetedFont)
        // 设置正文微博的属性文本
        statusAttrText = JCLEmoticonsManager.shared.emoticonString(string: model.text ?? "", font: originalFont)
        //缓存行高
        calcStatusRowHeight()
    }
    
    var description: String {
        
        return status.description
    }
    
    
   /// 底部工具栏文字/数字显示方法
   ///
   /// - Parameters:
   ///   - count: 所需显示的数量
   ///   - defaultStr: 默认文字
   /// - Returns: 返回样式
   fileprivate func countString(count: Int, defaultStr:String) -> String {
        
        if count == 0 {
            return defaultStr
        }
        
        if count < 10000 {
            return count.description
        }
        
        return String(format: "%0.2f", Double(count)/10000)
    }
    /// 计算配图视图高度方法
    ///
    /// - Returns: 配图视图的尺寸
    fileprivate func calsPictureSize(count: Int?) -> CGSize {
        
        if count == 0 || count == nil{
            return CGSize()
        }
     
        /// 返回图片九宫格的行数
        let row = (count! - 1) / 3 + 1
        /// 定义size的高度
        var height = jclPictureOuterMargin
        height += CGFloat(row) * jclPictureItemWidth
        height += CGFloat(row - 1) * jclPictureInterMargin
        /// 返回配图尺寸大小
        return CGSize(width: jclPictureViewWidth, height: height)
    }
    
    /// 更新单图尺寸
    ///
    /// - Parameter image: 单张图片
    func updateSingleImageSize(image:UIImage) {
        
        var size = image.size
        // 处理图片过大或者过小的显示
        let maxWidth:CGFloat = 300
        let minWidth:CGFloat = 40
        
        //当尺寸超过最大限度,等比例缩小显示
        if size.width > maxWidth {
            
            size.width = maxWidth
            
            size.height = size.width * image.size.height/image.size.width
        }
        // 当尺寸小于最小显示,等比例增大显示
        if size.width < minWidth {
            
            size.width = minWidth
            
            size.height = size.width * image.size.height/image.size.width
        }
        
        size.height += jclPictureOuterMargin
        
        pictureSize = size
        
        // 调用缓存行高
        calcStatusRowHeight()
   
    }
    
    /// 计算行高
    func calcStatusRowHeight () {
        
        var height:CGFloat = 0
        /*
         原创: 顶部分隔视图(12)+margin(12)+头像的高度(34)+margin+正文的高度(需要计算)+配图视图高度(计算)+margin+底部工具栏(35)
         转发: 顶部分隔视图(12)+margin+(12)+头像的高度(34)+margin+正文的高度+margin+margin+转发文本(需要计算)+配图视图高度+margin+底部工具栏(35)
         */
        /// 设置常量
        let margin = CGFloat(12)
        let iconHeight = CGFloat(34)
        let toolbarHeight = CGFloat(35)
        let textSize = CGSize(width: UIScreen.hm_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
 
        // 头部的高度
        height = margin * 2 + iconHeight + margin
        // 正文的高度
        if let text = statusAttrText {
            height += text.boundingRect(with: textSize, options: [.usesLineFragmentOrigin], context: nil).height
        }
        // 转发微博文本的高度
        if status.retweeted_status != nil {
            height += 2 * margin
            if let text = retweetedAttrText {
            
                height +=  text.boundingRect(with: textSize, options: [.usesLineFragmentOrigin], context: nil).height
            }
        }
        // 配图视图+底部工具栏
        height += pictureSize.height + margin + toolbarHeight
        // 赋值给属性
        rowHeight = height
    }
    
}
